<script setup lang="ts">
import EchartBar from '@/components/Echart/Bar/BarChart.vue'
import { storeToRefs } from 'pinia'
import { reactive, watch, computed, ref, nextTick } from 'vue'
import { useHomeStore } from '@/stores/module/home'
import { headersStore } from '@/stores/apiHeader'
import MembershipTable from '@/components/Echart/Table/MembershipTable.vue'
import AnimationDialog from '@/components/Dialog/AnimationDialog.vue'
import { useDialogStore } from '@/stores/dialog'
import {
  DEFAULT_ECHARTS_FONTSIZE,
  DIALOG_ECHARTS_FONTSIZE,
} from '@/Utils/setting'
const dialogStore = useDialogStore()
const { dialogInfo: yourNewName } = dialogStore
const fontSize = yourNewName.visible
  ? DIALOG_ECHARTS_FONTSIZE
  : DEFAULT_ECHARTS_FONTSIZE
const dialogInfoVisible = yourNewName.visible
const homeStore = useHomeStore() // 创建store实例
const { homeGlobalPerformanceYoY } = storeToRefs(homeStore)
const scopeCode = headersStore().scope_code
const language_id = headersStore().language_id

// 调整px到合适的大小
const changePixelSize = (px: number) => {
  // 获取当前屏幕宽度
  const width = document.body.clientWidth
  let res = (px / 1920) * width
  // 保留整数
  return parseInt(res.toFixed(0))
}
//遍历接口数据拿到想要的数据
const getAreaName = () => {
  const areaName = []
  homeGlobalPerformanceYoY.value?.info?.forEach((item) => {
    areaName.push(item.name)
  })
  return areaName
}
//遍历接口数据拿到想要的数据
const getAreaData = () => {
  const areaData = []
  homeGlobalPerformanceYoY.value?.info?.forEach((item) => {
    areaData.push(item.data)
  })
  return areaData
}
//遍历接口数据拿到想要的数据
const getAreaSubTitles = () => {
  const areaSubTitles = []
  homeGlobalPerformanceYoY.value?.sub_titles?.forEach((item) => {
    areaSubTitles.push(item)
  })
  return areaSubTitles
}

//echarts配置项
const chartData = reactive({
  name: 'globalPerformanceYoY',

  legend: {
    top: '2%',
    data: getAreaName(),
    itemWidth: changePixelSize(7.2),
    itemHeight: changePixelSize(10.5),
    textStyle: {
      fontSize: changePixelSize(fontSize),
      lineHeight: changePixelSize(15),
      color: '#fff',
    },
  },
  grid: {
    left: '3%',
    right: '15%',
    bottom: '5%',
    top: '20%',
    containLabel: true,
  },

  xAxis: {
    type: 'category',
    data: getAreaSubTitles(),
    axisTick: {
      show: false,
    },
    axisLabel: {
      color: '#fff', // 设置 X 轴标签字体颜色

      interval: 0, // 强制显示所有标签
      fontSize: changePixelSize(fontSize),
      margin: changePixelSize(5), // 调整标签与轴线的距离
    },
  },
  yAxis: {
    type: 'value',
    axisLine: {
      show: true,
    },
    splitLine: {
      show: false,
    },
    axisLabel: {
      fontSize: changePixelSize(fontSize),
      color: '#08ffff',
      margin: changePixelSize(5), // 调整标签与轴线的距离
    },
  },
  dataZoom: [
    {
      type: 'inside',
      show: false,
      xAxisIndex: [0],
      zoomOnMouseWheel: false, // 禁止鼠标滚轮缩放
      moveOnMouseWheel: true, // 开启鼠标滚轮窗口平移
      moveOnMouseMove: true, // 开启鼠标移动窗口平移
      start: 0,
      end:
        scopeCode.length == 1 && language_id == 'zh-cn'
          ? changePixelSize(125)
          : '' || (scopeCode.length == 1 && language_id == 'en-us')
          ? changePixelSize(70)
          : '' || (scopeCode.length == 10 && language_id == 'zh-cn')
          ? changePixelSize(10)
          : '' || (scopeCode.length == 10 && language_id == 'en-us')
          ? changePixelSize(7)
          : '' || (scopeCode.length == 2 && language_id == 'zh-cn')
          ? changePixelSize(125)
          : '' || (scopeCode.length == 2 && language_id == 'en-us')
          ? changePixelSize(40)
          : '' || (scopeCode.length == 5 && language_id == 'en-us')
          ? changePixelSize(70)
          : '' || (scopeCode.length == 5 && language_id == 'zh-cn')
          ? changePixelSize(125)
          : '',
      showDataShadow: false,

      textStyle: {
        color: '#ccd7d7',
      },
    },
  ],
  series: [
    {
      type: 'bar',
      name: getAreaName()[0],
      data: getAreaData()[0],
      barWidth:
        dialogInfoVisible == false ? changePixelSize(10) : changePixelSize(25),
      showBackground: true,
      itemStyle: {
        color: '#193aec', // 设置柱子颜色
      },
    },
    {
      type: 'bar',
      name: getAreaName()[1],
      data: getAreaData()[1],
      barWidth:
        dialogInfoVisible == false ? changePixelSize(10) : changePixelSize(25),
      showBackground: true,
      itemStyle: {
        color: '#00b7ee', // 设置柱子颜色
      },
    },
  ],
})
const pastTile = getAreaName()[0]
const nowTile = getAreaName()[1]
const tableBodyData = homeGlobalPerformanceYoY.value?.sub_titles?.map(
  (name, index) => ({
    name,
    nowValue: [...getAreaData()[0]][index],
    pastValue: [...getAreaData()[1]][index],
  })
)

const title = ref(homeGlobalPerformanceYoY.value.title)
const dialogInfo = {
  title: title.value, // 标题
  style: 'rm', // 弹窗位置，例如右中
  width: '50%', // 宽度
}
const dialog = ref(null)
const btn = async () => {
  await nextTick()
  dialog.value.changeDialogVisible(true)
}
watch(
  () => homeGlobalPerformanceYoY.value,
  () => {
    chartData.legend.data = getAreaName()
    chartData.xAxis.data = getAreaSubTitles()
    chartData.series[0].name = getAreaName()[0]
    chartData.series[0].data = getAreaData()[0]
    chartData.series[1].name = getAreaName()[1]
    chartData.series[1].data = getAreaData()[1]
  },
  { deep: true }
)
</script>
<template>
  <card-box componentName="globalPerformanceYoY">
    <template v-slot:title>
      <h1
        :class="{
          subtitle: dialogInfoVisible,
        }"
      >
        {{ homeGlobalPerformanceYoY.title }}
      </h1>
    </template>

    <button
      class="see-more-btn"
      size="small"
      @click="btn"
      v-if="
        homeGlobalPerformanceYoY.sub_titles.length > 5 &&
        yourNewName.visible == false
      "
    >
      {{ $t('bi_finance_details') }}
    </button>
    <EchartBar
      :chart-data="chartData"
      v-if="homeGlobalPerformanceYoY.info[0]?.data?.length"
    ></EchartBar>
    <div v-else class="default-img">
      <p>{{ $t('bi_common_nodata') }}</p>
    </div>
  </card-box>

  <AnimationDialog
    ref="dialog"
    :dialogInfo="dialogInfo"
    class="transactionData"
    style="background-color: rgba(11, 31, 86, 0.5)"
  >
    <MembershipTable
      :chart-data="tableBodyData"
      :getAreaName="getAreaName"
      :pastTile="pastTile"
      :nowTile="nowTile"
    />
  </AnimationDialog>
</template>
<style lang="scss" scoped>
.default-img {
  width: 100%;
  text-align: center;
  padding: 120px 0;
  background-image: url('@/assets/images/canvsimage/default.png');
  background-repeat: no-repeat;
  background-position: center;

  font-size: 16px;
  color: #00f6ff;
}
.see-more-btn {
  position: absolute;
  left: 0;
  z-index: 100;
  width: 100px;
  height: 35px;
  text-align: center;
  margin-top: -35px;
  font-size: 10px;
  background-color: rgba(0, 53, 171, 0.5);
  border: 1px solid #0b45ca;
  color: #00f6ff;
  cursor: pointer;
}
.subtitle {
  font-size: 30px;
}
</style>
